home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / SCSI Samples 1.0 / SCSI Async Sample 06⁄15 ƒ / Src / DialogUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  4.1 KB  |  227 lines  |  [TEXT/KAHL]

  1. #include "DialogUtilities.h"
  2. #include <Packages.h>
  3.  
  4. /*
  5.  * User clicked on a checkbox.
  6.  */
  7. void
  8. SelectCheckBox(
  9.         DialogPtr            theDialog,
  10.         short                itemNumber,
  11.         Boolean                *value
  12.     )
  13. {
  14.         short                temp;
  15.         
  16.         temp = *value;
  17.         SelectRadioButton(theDialog, itemNumber, itemNumber, itemNumber, &temp);
  18.         *value = temp;
  19. }
  20.  
  21. /*
  22.  * Initalize a check box.
  23.  */
  24. void
  25. SetCheckBox(
  26.         DialogPtr            theDialog,
  27.         short                itemNumber,
  28.         Boolean                value
  29.     )
  30. {
  31.         SetRadioButtons(theDialog, itemNumber, itemNumber, value);
  32. }
  33.  
  34. /*
  35.  * User clicked on a radio button, change the group.
  36.  */
  37. void
  38. SelectRadioButton(
  39.         DialogPtr            theDialog,
  40.         short                first,
  41.         short                last,
  42.         short                item,
  43.         short                *value        /* Has/gets zero to last-1    */
  44.     )
  45. {
  46.         if (item < first || item > last)
  47.             ;
  48.         else if (last > first) {        /* Radio button                */
  49.             SetDialogButton(theDialog, *value + first, kActiveButton);
  50.             SetDialogButton(theDialog, item, kCheckedButton);
  51.             *value = item - first;
  52.         }
  53.         else {                            /* Check box                */
  54.             *value = !(*value);
  55.             SetDialogButton(
  56.                 theDialog,
  57.                 first,
  58.                 (*value != 0) ? kCheckedButton : kActiveButton
  59.             );
  60.         }
  61. }
  62.  
  63. /*
  64.  * Clear out a radio button group, then check the desired one.
  65.  */
  66. void
  67. SetRadioButtons(
  68.         DialogPtr            theDialog,
  69.         short                first,
  70.         short                last,
  71.         short                value
  72.     )
  73. {
  74.         short                i;
  75.         
  76.         if (first == last) {            /* Check box                */
  77.             SetDialogButton(
  78.                 theDialog,
  79.                 first,
  80.                 (value != 0) ? kCheckedButton : kActiveButton
  81.             );
  82.         }
  83.         else {
  84.             for (i = first; i <= last; i++) {
  85.                 SetDialogButton(theDialog, i, kActiveButton);
  86.                 SetDialogButton(theDialog, first + value, kCheckedButton);
  87.             }
  88.         }
  89. }
  90.  
  91. void
  92. SetDialogButton(
  93.         DialogPtr            theDialog,
  94.         short                item,
  95.         short                value
  96.     )
  97. {
  98.         short                type;
  99.         Handle                handle;
  100.         Rect                box;
  101.         
  102.         GetDItem(theDialog, item, &type, &handle, &box);
  103.         switch (type & ~itemDisable) {
  104.         case (ctrlItem + btnCtrl):
  105.             if (value == kActiveButton)
  106.                 HiliteControl((ControlHandle) handle, 0);
  107.             else if (value == kDisabledButton) {
  108.                 HiliteControl((ControlHandle) handle, 255);
  109.             }
  110.             break;
  111.         case (ctrlItem + chkCtrl):
  112.         case (ctrlItem + radCtrl):
  113.             if (value == kDisabledButton)
  114.                 HiliteControl((ControlHandle) handle, 255);
  115.             else {
  116.                 HiliteControl((ControlHandle) handle, 0);
  117.                 SetCtlValue((ControlHandle) handle, value);
  118.             }
  119.             break;
  120.         case (userItem):
  121.             if (((WindowPeek) theDialog)->visible
  122.              && (value == kActiveButton || value == kCheckedButton))
  123.                 InvertRect(&box);
  124.             break;
  125.         }    
  126. }
  127.  
  128. /*
  129.  * Return the theDialog button's value (kActiveButton, or kCheckedButton).
  130.  */
  131. short
  132. GetDialogButton(
  133.         DialogPtr            theDialog,
  134.         short                item
  135.     )
  136. {
  137.         short                type;
  138.         ControlHandle        handle;
  139.         Rect                box;
  140.         
  141.         GetDItem(theDialog, item, &type, (Handle *) &handle, &box);
  142.         return (GetCtlValue(handle));
  143. }
  144.  
  145. void
  146. EnableDialogItem(
  147.         DialogPtr            theDialog,
  148.         short                item,
  149.         Boolean                enableItem
  150.     )
  151. {
  152.         short                type;
  153.         ControlHandle        handle;
  154.         Rect                box;
  155.         Boolean                isEnabled;
  156.         
  157.         GetDItem(theDialog, item, &type, (Handle *) &handle, &box);
  158.         isEnabled = (type & itemDisable) == 0;
  159.         if (isEnabled != enableItem) {
  160.             if (enableItem)
  161.                 type &= ~itemDisable;
  162.             else {
  163.                 type |= itemDisable;
  164.             }
  165.             SetDItem(theDialog, item, type, (Handle) handle, &box);
  166.             switch (type & ~itemDisable) {
  167.             case (ctrlItem + btnCtrl):
  168.             case (ctrlItem + chkCtrl):
  169.             case (ctrlItem + radCtrl):
  170.             case (ctrlItem + resCtrl):
  171.                 if (enableItem)
  172.                     HiliteControl(handle, 0);
  173.                 else {
  174.                     HiliteControl(handle, 255);
  175.                 }
  176.                 break;
  177.             }
  178.         }    
  179.  
  180. }
  181.  
  182. signed long
  183. GetDialogValue(
  184.         DialogPtr            theDialog,
  185.         short                item
  186.     )
  187. {
  188.         short                type;
  189.         Handle                handle;
  190.         Rect                box;
  191.         signed long            result;
  192.         Str255                work;
  193.  
  194.         GetDItem(theDialog, item, &type, &handle, &box);    
  195.         GetIText(handle, work);
  196.         StringToNum(work, &result);
  197.         return (result);
  198. }
  199.  
  200. void
  201. SetDialogValue(
  202.         DialogPtr            theDialog,
  203.         short                item,
  204.         signed long            value
  205.     )
  206. {
  207.         Str255                work;
  208.  
  209.         NumToString(value, work);
  210.         SetDialogText(theDialog, item, work);
  211. }
  212.  
  213. void
  214. SetDialogText(
  215.         DialogPtr            theDialog,
  216.         short                item,
  217.         ConstStr255Param    theText
  218.     )
  219. {
  220.         short                type;
  221.         Handle                handle;
  222.         Rect                box;
  223.  
  224.         GetDItem(theDialog, item, &type, &handle, &box);
  225.         SetIText(handle, theText);
  226. }
  227.